package cn.wjdiankong.levideo.utils;

import com.ss.android.common.applog.GlobalContext;

import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.activity.MainActivity;
import net.youmi.android.nm.bn.BannerManager;
import net.youmi.android.nm.bn.BannerViewListener;
import net.youmi.android.nm.sp.SplashViewSettings;
import net.youmi.android.nm.sp.SpotListener;
import net.youmi.android.nm.sp.SpotManager;
import net.youmi.android.nm.sp.SpotRequestListener;
import net.youmi.android.nm.vdo.VideoAdListener;
import net.youmi.android.nm.vdo.VideoAdManager;
import net.youmi.android.nm.vdo.VideoAdRequestListener;
import net.youmi.android.nm.vdo.VideoAdSettings;
import net.youmi.android.nm.vdo.VideoInfoViewBuilder;

public class AdUtils {
	
	private final static boolean isSwitch = false;//广告开关

	public interface AdListener{
		void showSucc();
		void showFail();
		void click();
		void close();
	}

	public static void preLoadVideoAd(){
		if(!isSwitch){
			return;
		}
		//预加载视频广告
		VideoAdManager.getInstance(GlobalContext.getContext()).requestVideoAd(GlobalContext.getContext(),new VideoAdRequestListener(){
			@Override
			public void onRequestFailed(int arg0) {
			}
			@Override
			public void onRequestSuccess() {
			}
		});
	}
	
	public static void preLoadSplashAd(){
		if(!isSwitch){
			return;
		}
		//预加载开平广告
		SpotManager.getInstance(GlobalContext.getContext()).requestSpot(new SpotRequestListener(){
			@Override
			public void onRequestFailed(int arg0) {
			}

			@Override
			public void onRequestSuccess() {
			}
		});
	}

	public static void showSplashAd(ViewGroup group , final AdListener listener){
		if(!isSwitch){
			return;
		}
		SplashViewSettings splashViewSettings = new SplashViewSettings();
		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
		layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		splashViewSettings.setTargetClass(MainActivity.class);//跳转的页面
		splashViewSettings.setSplashViewContainerAndLayoutParams(group, layoutParams);
		splashViewSettings.setAutoJumpToTargetWhenShowFailed(false);
		SpotManager.getInstance(GlobalContext.getContext()).showSplash(
				GlobalContext.getContext(), splashViewSettings, new SpotListener() {
					@Override
					public void onShowSuccess() {
						Logger.log("get ad succ...");
						UmengReportUtils.report(UmengReportUtils.AD_SPLASH_SHOW);
						if(listener != null){
							listener.showSucc();
						}
					}

					@Override
					public void onShowFailed(int errorCode) {
						Logger.log("get ad err:"+errorCode);
						UmengReportUtils.report(UmengReportUtils.AD_SPLASH_ERROR);
						if(listener != null){
							listener.showFail();
						}
					}

					@Override
					public void onSpotClosed() {
						Logger.log("close ad");
						UmengReportUtils.report(UmengReportUtils.AD_SPLASH_CLOSE);
						if(listener != null){
							listener.close();
						}
					}

					@Override
					public void onSpotClicked(boolean isWebPage) {
						Logger.log("click ad");
						UmengReportUtils.report(UmengReportUtils.AD_SPLASH_CLICK);
						if(listener != null){
							listener.click();
						}
					}
				});
	}

	public static void showVideoAd(final ViewGroup adVideoLayout, final ViewGroup videoInfoLayout, final AdListener adListener){
		if(!isSwitch){
			return;
		}
		// 设置视频广告
		VideoAdSettings videoAdSettings = new VideoAdSettings();
		// 设置信息流视图，将图标，标题，描述，下载按钮对应的ID传入
		final VideoInfoViewBuilder videoInfoViewBuilder = VideoAdManager.getInstance(GlobalContext.getContext())
				.getVideoInfoViewBuilder(
						GlobalContext.getContext()).setRootContainer(videoInfoLayout)
				.bindAppIconView(R.id
						.info_iv_icon)
				.bindAppNameView(R.id
						.info_tv_title)
				.bindAppDescriptionView(R.id
						.info_tv_description)
				.bindDownloadButton(R.id
						.info_btn_download);
		// 注意：请在UI线程调用该方法
		// 获取原生视频控件
		View nativeVideoAdView = VideoAdManager.getInstance(GlobalContext.getContext())
				.getNativeVideoAdView(GlobalContext.getContext(), videoAdSettings, new VideoAdListener() {
					@Override
					public void onPlayStarted() {
						Logger.log("video ad start");
						UmengReportUtils.report(UmengReportUtils.AD_VIDEO_SHOW);
						if (videoInfoLayout != null && videoInfoLayout.getVisibility() != View.VISIBLE) {
							videoInfoLayout.setVisibility(View.VISIBLE);
						}
						if(adListener != null){
							adListener.showSucc();
						}
					}

					@Override
					public void onPlayInterrupted() {
						Logger.log("video ad intercept");
						UmengReportUtils.report(UmengReportUtils.AD_VIDEO_CLOSE);
						if (videoInfoLayout != null && videoInfoLayout.getVisibility() != View.GONE) {
							videoInfoLayout.setVisibility(View.GONE);
						}
						videoInfoViewBuilder.release();
						// 移除原生视频控件
						if (adVideoLayout != null) {
							adVideoLayout.removeAllViews();
							adVideoLayout.setVisibility(View.GONE);
						}
					}

					@Override
					public void onPlayFailed(int errorCode) {
						Logger.log("video ad fail");
						UmengReportUtils.report(UmengReportUtils.AD_VIDEO_ERROR);
						if(adListener != null){
							adListener.showFail();
						}
					}

					@Override
					public void onPlayCompleted() {
					}
				});

		if (adVideoLayout != null) {
			final RelativeLayout.LayoutParams params =
					new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
							ViewGroup.LayoutParams.WRAP_CONTENT);
			if (nativeVideoAdView != null) {
				adVideoLayout.removeAllViews();
				// 添加原生视频广告
				adVideoLayout.addView(nativeVideoAdView, params);
				adVideoLayout.setVisibility(View.VISIBLE);
			}
		}
	}

	public static void showBannerAd(final ViewGroup bannerLayout, final AdListener listener){
		if(!isSwitch){
			return;
		}
		// 获取广告条
		View bannerView = BannerManager.getInstance(GlobalContext.getContext()).getBannerView(
				GlobalContext.getContext(),new BannerViewListener(){
					@Override
					public void onRequestFailed() {
						UmengReportUtils.report(UmengReportUtils.AD_BANNER_ERROR);
						if(listener != null){
							listener.showFail();
						}
						if(bannerLayout != null){
							bannerLayout.removeAllViews();
							bannerLayout.setVisibility(View.GONE);
						}
					}

					@Override
					public void onRequestSuccess() {
						UmengReportUtils.report(UmengReportUtils.AD_BANNER_SHOW);
						if(bannerLayout != null){
							bannerLayout.setVisibility(View.VISIBLE);
						}
						if(listener != null){
							listener.showSucc();
						}
					}

					@Override
					public void onSwitchBanner() {
					}});
		// 将广告条加入到布局中
		if(bannerView != null){
			bannerLayout.addView(bannerView);
			bannerLayout.setVisibility(View.VISIBLE);
		}
	}

}
